/*
 * @Author: your name
 * @Date: 2021-08-31 09:35:31
 * @LastEditTime: 2021-09-07 10:53:35
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \index\src\ys\cesium\DataTypeLoad.js
 */
import * as Cesium from "cesium";

function DataTypeLoadInit(viewer) {
  this.viewer = viewer;
}

/**
 * @description: 添加影像图层
 * @param {*}
 * @return {*}
 */
DataTypeLoadInit.prototype.DataImageLayerLoad = function(
  imageryProviderUrl,
  imgLayerIndex
) {
  let viewer = this.viewer;
  let imageLayers = viewer.scene.imageryLayers;
  console.log(imageLayers);
  let imageLayerResult = imageLayers.addImageryProvider(
    new Cesium.UrlTemplateImageryProvider(
      {
        url: imageryProviderUrl,
      },
      imgLayerIndex
    )
  );
  return imageLayerResult;
};

/**
 * @description: 返回影像图层集合对象
 * @param {*}
 * @return {*}
 */
DataTypeLoadInit.prototype.DataImageryLayerCollection = function() {
  return this.viewer.scene.imageryLayers;
};

/**
 * @description: 所有影像数据显示控制
 * @param {*}
 * @return {*}
 */
DataTypeLoadInit.prototype.DataImageryLayerCollectionControl = function(
  controlStatus
) {
  let imageryLayersArray = this.viewer.scene.imageryLayers._layers;
  console.log(imageryLayersArray);
  imageryLayersArray.forEach((element) => {
    //跳过底图， _isBaseLayer
    if (element?._isBaseLayer) {
      return;
    }
    element.show = controlStatus;
  });
};

/**
 * @description: 调整影像图层位置为最上层
 * @param {*} adjustLayer
 * @return {*}
 */
DataTypeLoadInit.prototype.DataImageLayerPositionTop = function(adjustLayer) {
  let viewer = this.viewer;
  let imageLayers = viewer.scene.imageryLayers;
  imageLayers.raiseToTop(adjustLayer);
};

/**
 * @description: 调整影像图层位置为向上一层
 * @param {*} adjustLayer
 * @return {*}
 */
DataTypeLoadInit.prototype.DataImageLayerPositionUpper = function(adjustLayer) {
  let viewer = this.viewer;
  let imageLayers = viewer.scene.imageryLayers;
  imageLayers.raise(adjustLayer);
};

/**
 * @description: 关闭当前影像图层显示
 * @param {*} adjustLayer
 * @return {*}
 */
DataTypeLoadInit.prototype.DataImageLayerShowControl = function(adjustLayer) {
  adjustLayer.show = !adjustLayer.show;
};

/**
 * @description: 调整跳转位置
 * @param {*} positionObj
 * @return {*}
 */
DataTypeLoadInit.prototype.PositionAdjust = function(positionObj) {
  let viewer = this.viewer;
  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(
      positionObj?.x,
      positionObj?.y,
      positionObj?.z
    ),
  });
};

/**
 * @description: 添加显示3D title数据
 * @param {*} Data3DTitleUrl
 * @return {*}
 */
DataTypeLoadInit.prototype.Data3DTitleLoad = function(Data3DTitleUrl) {
  let viewer = this.viewer;
  let data3DTitle = viewer.scene.primitives.add(
    new Cesium.Cesium3DTileset({
      url: Data3DTitleUrl,
    })
  );
  viewer.zoomTo(data3DTitle);
  return data3DTitle;
};

/**
 * @description: 3D title全部数据显示控制
 * @param {*}
 * @return {*}
 */
DataTypeLoadInit.prototype.Data3DTitleControl = function(
  data3DTitles,
  controlStatus
) {
  // 循环全部3d title对象
  for (const key in data3DTitles) {
    if (Object.hasOwnProperty.call(data3DTitles, key)) {
      const element = data3DTitles[key];
      element.show = controlStatus;
    }
  }
};

/**
 * @description: 添加显示terrain数据（只能修改一个数据源）
 * @param {*} DataTerrainUrl
 * @return {*}
 */
DataTypeLoadInit.prototype.DataTerrainLoad = function(DataTerrainUrl) {
  let viewer = this.viewer;
  let dataTerrain = (viewer.terrainProvider = new Cesium.CesiumTerrainProvider({
    url: DataTerrainUrl,
  }));
  return dataTerrain;
};

/**
 * @description: 添加显示geojson数据
 * @param {*}
 * @return {*}
 */
DataTypeLoadInit.prototype.DataGeoJSONLoad = function(
  DataGeoJSONUrl,
  viewer = this.viewer
) {
  const geojsonTemp = viewer.dataSources.add(
    Cesium.GeoJsonDataSource.load(DataGeoJSONUrl, {
      stroke: Cesium.Color.HOTPINK,
      fill: "#bfa",
      strokeWidth: 3,
    })
  );
  geojsonTemp.then(function(data) {
    return data;
  });
  return geojsonTemp;
};

/**
 * @description: geojson全部数据显示控制
 * @param {*}
 * @return {*}
 */
DataTypeLoadInit.prototype.DataGeoJSONControl = function(
  dataGeoJSONs,
  controlStatus
) {
  // 循环全部GeoJSON对象
  for (const key in dataGeoJSONs) {
    if (dataGeoJSONs[key] && Object.hasOwnProperty.call(dataGeoJSONs, key)) {
      dataGeoJSONs[key].show = controlStatus;
    }
  }
};

export { DataTypeLoadInit };
